//
//  ViewController.m
//  SmartTalk
//
//  Created by 陈勃 on 14-12-19.
//  Copyright (c) 2014年 gameabc. All rights reserved.
//

#import "ViewController.h"
#import "NetWorkManager.h"
#import "NSObject+JSON.h"
#import "loginViewController.h"
#import "ChatManager.h"
#import "chattingViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSArray *friendList;
}
@end

@implementation ViewController

-(void)connect
{
    [[NetWorkManager sharedInstance] connect];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return friendList.count;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    [cell.textLabel setText:friendList[indexPath.row][@"name"]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChattingViewController *controller = [[ChattingViewController alloc] initWithNibName:@"chattingViewController" bundle:nil];
    NSString *userid = [NSString stringWithFormat:@"%@",friendList[indexPath.row][@"userid"]];
    [controller setTouserid:userid];
    [self.navigationController pushViewController:controller animated:YES];
}

-(IBAction)regAction:(id)sender
{
    [namefield resignFirstResponder];
    [pwdfield resignFirstResponder];
    NSDictionary *dic  = @{@"name":namefield.text,@"pwd":pwdfield.text,@"phone":@"15012345678",@"action":@(0x010001)};
    [[NetWorkManager sharedInstance] sendContent:[dic JSONRepresentation]];

}



-(IBAction)loadFriends
{
    [[ChatManager sharedInstance] loadFriendList:0 callback:^(NSDictionary *resp) {
        int code = [[resp objectForKey:KEY_RESULT_CODE] intValue];
        if (code == 0) {
            friendList = [resp objectForKey:KEY_RESULT_ARRAY];
        }
        [contentTable reloadData];
    }];
}

-(IBAction)loginAction:(id)sender
{
    [namefield resignFirstResponder];
    [pwdfield resignFirstResponder];
    [[ChatManager sharedInstance] loginwith:namefield.text pwd:pwdfield.text callback:^(NSDictionary *resp) {
        int code = [[resp objectForKey:KEY_RESULT_CODE] intValue];
        if (code == 0) {
            NSLog(@"登录成功");
            [self loadFriends];
        }
    }];
    // [[NetWorkManager sharedInstance] sendContent:[dic JSONRepresentation]];
}


- (void)viewDidLoad {
    friendList = @[];
    [contentTable setDataSource:self];
    [contentTable setDelegate:self];
    [self connect];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
